package org.kabeja.parser.entities;

import org.kabeja.dxf.entity.DXFEntity;
import org.kabeja.dxf.entity.DXFRegion;
import org.kabeja.parser.DXFValue;

/**
 * @author shutao
 * @since 2022/4/25 11:51
 */
public class DXFRegionHandler extends AbstractEntityHandler {
    protected static final int DATA = 1;
    protected static final int APPEND_DATA = 3;
    protected DXFRegion region;
    protected StringBuffer data = new StringBuffer();

    public DXFRegionHandler() {
    }

    @Override
    public String getDXFEntityName() {
        return "REGION";
    }

    @Override
    public void startDXFEntity() {
        this.region = new DXFRegion();
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        switch(var1) {
            case 1:
                this.checkBuffer();
                this.data.append(var2.getValue());
                break;
            case 3:
                this.data.append(var2.getValue());
                break;
            default:
                break;
        }

    }

    @Override
    public void parseObject(int var1, DXFValue var2, int type) {
        super.parseObjectProperty(var1, var2, type, null);
    }

    @Override
    public DXFEntity getDXFEntity() {
        return this.region;
    }

    @Override
    public void endDXFEntity() {
        this.checkBuffer();
    }

    @Override
    public boolean isFollowSequence() {
        return false;
    }

    protected String decodeDATA(String var1) {
        char[] var2 = var1.toCharArray();
        StringBuffer var3 = new StringBuffer();

        for(int var4 = 0; var4 < var2.length; ++var4) {
            if (Character.isWhitespace(var2[var4])) {
                var3.append(' ');
            } else {
                int var5 = Math.abs(var2[var4] - 159);
                var3.append((char)var5);
            }
        }

        return var3.toString();
    }

    protected void checkBuffer() {
        if (this.data.length() > 0) {
            this.region.appendACISDATA(this.decodeDATA(this.data.toString()));
            this.data.delete(0, this.data.length());
        }

    }
}
